<?php
/**
 * 后台登陆
 */
namespace app\admin\controller;
use think\Controller;
use think\Loader;
use think\Request;
use think\Db;
class Category extends Controller
{
    // 登陆
    public function index(){
        return $this->fetch();
    }
    //eq ==    neq !=    like 模糊
    public function add(){
        $list = DB::name('category');
        $where['parent_id'] = array('eq', "0", 'or');
        $res["data"]=$list->where($where)->select();
        $this->assign('list', $res);
        $request = Request::instance();
        if($request->isAjax()){
            $data = [       //接受传递的参数
                'name' => input('name'),
                'parent_id' => input('parent_id'),
                'is_parent' => input('is_parent'),
            ];
            
        /*  Db('表名') 数据库助手函数*/
            if($list -> insert($data)){  
                 //添加数据
                 return json(array('code' => 200, 'msg' => '添加成功'));
                return $this->success('添加成功');    //成功后跳转  lst 界面
                 
            }else{
                 return json(array('code' => 0, 'msg' => '添加失败'));
                return $this->error('添加管理员失败');
            }
            return;
        }
        return $this->fetch();
    }
     // 用户
     public function del()
     {
         $id = input('post.id');
         $del = model('Category')->delId($id);
         if ($del) {
             return 1;
         } else {
             return 2;
         }
     }
    public function categorylist()
    {
        $page = input('get.page');
        $pagesize = input('get.limit');
        $start = $pagesize*($page-1);
        $end = $pagesize*$page;
        $list = DB::name('category');
        $res["data"]=$list->limit($start.','.$pagesize)->select();
        $res["code"]=0;
        $res["msg"]="成功";
        $res["count"]=$list->count();
        // var_dump($res);
        return $res;
    }  
}